package com.yyds.blackmagic.config;

import com.yyds.blackmagic.interceptor.JwtInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @Author ccc
 * @Date 2021/12/13
 * @Descripton
 */
@Configuration
@Slf4j
public class JwtConfig implements WebMvcConfigurer {
    @Autowired
    private JwtInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)//配置拦截器
                .addPathPatterns("/api/**")//配置拦截路径
                .excludePathPatterns("/api/login","/api/register");//配置放行路径
    }

    //全局跨域处理
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")  //跨域处理路径
                .allowedOrigins("*")  //允许跨域
                .allowCredentials(true)  //使客户端信任
                //允许跨域处理的动作
                .allowedMethods("GET","POST","DELETE","PUT","PATCH","OPTIONS","HEAD")
                .maxAge(3600*24);
    }
}
